using UnityEngine;
using System.Collections;

[System.Serializable]
public class RotateObjectOnTrigger : MonoBehaviour {
	
	/**
	 * http://vimeo.com/16023054
	 * Author: Stefan Boeykens
	 * Adapted to C# by: André Nilsen
	 */ 
	
	public float AngleX = 0.0f;
	public float AngleY = 0.0f;
	public float AngleZ = 0.0f;
	
	private float targetValue = 0.0f;
	private float currentValue = 0.0f;
	private float easing = 0.1f;
	
	// Added by André Nilsen
	private bool useKeyPressed = true;
	private bool rotated = false;	
	private string message = "";
	
	public GameObject Target;
	
	public bool debug = false;
	
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		//useKeyPressed = Input.GetKeyUp("e");
		
		currentValue = currentValue + (targetValue-currentValue) * easing;
		
		Target.transform.rotation = Quaternion.identity;
		Target.transform.Rotate(0, currentValue, 0);
	}
	
	void OnTriggerEnter(Collider other) {
		// If sentence added by André Nilsen
		if (other.tag == "Player") {
			if (!rotated) {
				targetValue = AngleY;
				currentValue = 0;
				rotated = true;
			} else {
				currentValue = AngleY;
				targetValue = 0.0f;
				rotated = false;
			}
		}
		
		message = "Object is rotated? " + rotated;
		
		if (debug) {
			//message = other.tag + " entered the trigger";	
		}
	}
	
	// Added by André Nilsen
	/*void OnTriggerStay(Collider other) {
		// If sentence added by André Nilsen
		if (other.tag == "Player" && useKeyPressed) {
			if (!rotated) {
				targetValue = AngleY;
				currentValue = 0;
				rotated = true;
			} else {
				currentValue = AngleY;
				targetValue = 0.0f;
				rotated = false;
			}
		}
	} */
	
	
	/* void OnTriggerExit(Collider other) {
		// If sentence added by André Nilsen
		if (other.tag == "Player" && useKeyPressed) {
			currentValue = AngleY;
			targetValue = 0.0f;
		}
	} */
	
	void OnGUI() {
		if (debug) {
			GUI.Box(new Rect(200, 200, 200, 100), message);	
		}
	}
}
